package com.example.studentapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.studentapp.R;
import com.example.studentapp.beans.Money;
import com.example.studentapp.money.DetailRecordActivity;

import java.util.List;

public class MoneyAdapter extends BaseAdapter {
    Context context;
    List<Money> list;

    public MoneyAdapter(Context context,List<Money> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_money,null);
        TextView item_diary_titleTV = view.findViewById(R.id.item_money_titleTV);
        String title ="";
        Money money = list.get(i);
        double out = money.getCloth()+ money.getFood() + money.getHouse() + money.getUse() + money.getWalk() + money.getOther();
        double in = money.getGet();
        if(out>0&&in>0){
            title=context.getString(R.string.money_out_o)+out+context.getString(R.string.money_yuan)+","
                    +context.getString(R.string.money_in_o)+in+context.getString(R.string.money_yuan);
        }
        else if(out==0&&in>0){
            title=context.getString(R.string.money_in_o)+in+context.getString(R.string.money_yuan);

        }else if(out>0&&in==0){
            title=context.getString(R.string.money_out_o)+out+context.getString(R.string.money_yuan);

        }
        item_diary_titleTV.setText(title);
        TextView item_diary_dateTV = view.findViewById(R.id.item_money_dateTV);
        item_diary_dateTV.setText(list.get(i).getDate());
        ImageView item_diary_moreIV = view.findViewById(R.id.item_money_moreIV);
        item_diary_moreIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DetailRecordActivity.class);
                intent.putExtra("id",list.get(i).getId());
                intent.putExtra("title",list.get(i).getId());
                intent.putExtra("out",out);
                context.startActivity(intent);
            }
        });

        return view;
    }
}
